//
//  JobMiddleViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/8/14.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "JobMiddleViewController.h"
#import "JobDetailViewController.h"
#import "PreviewResumeViewController.h"

@interface JobMiddleViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) UITableView *jobListTableView;
@end

@implementation JobMiddleViewController

-(void)viewWillAppear:(BOOL)animated
{
	[self getJobInfo];
}

- (void)viewDidLoad {
	[super viewDidLoad];
	[self buildUI];
	[self setupRefresh];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

- (void)buildUI{
	_jobListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -5, SCREEN_WIDTH, SCREEN_HEIGHT-60) style:UITableViewStylePlain];
	[_jobListTableView registerClass:[JobTableViewCell class] forCellReuseIdentifier:@"JobCell"];
	[_jobListTableView registerNib:[UINib nibWithNibName:@"ApplicantTableViewCell" bundle:nil] forCellReuseIdentifier:@"ApplicantCell"];
	//_jobListTableView.estimatedRowHeight = 440.0f;
	_jobListTableView.delegate = self;
	_jobListTableView.dataSource = self;
	_jobListTableView.scrollEnabled = NO;
	[self.view addSubview:_jobListTableView];
}

- (void)getJobInfo
{
	[[NetworkTool sharedTool] requestWithName:@"获取首页中间推荐信息（岗位）" URLString:APIRecommendIndex parameters:nil method:@"POST" callBack:^(id responseObject) {
		
		NSError *error = nil;
		//_jobArray = [NSJSONSerialization JSONObjectWithData:responseObject[@"data"][@"recruit"] options:NSJSONReadingMutableContainers error:&error];
		NSArray *array = responseObject[@"data"][@"recruit"];
		[self.jobArray removeAllObjects];
		for (NSDictionary *dict in array)
		{
			JobInfoModel *jobInfo = [[JobInfoModel alloc] init];
			[jobInfo setValuesForKeysWithDictionary:dict];
			[self.jobArray addObject:jobInfo];
		}
		if (_jobArray) {
			NSLog(@"职位信息是%@",_jobArray);
			NSLog(@"有%ld个数据", _jobArray.count);
			[self.jobListTableView reloadData];
			
		}
		else
			NSLog(@"没有拿到职位信息");
		if (error) {
			NSLog(@"职位信息解析error = %@", error);
		}
	}];
}

- (void)setupRefresh {
	NSLog(@"setupRefresh -- 下拉刷新");
	UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
	[refreshControl addTarget:self action:@selector(refreshClick:) forControlEvents:UIControlEventValueChanged];
	refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"正在刷新"];
	refreshControl.tintColor = [UIColor blackColor];
	[self.jobListTableView addSubview:refreshControl];
}
// 下拉刷新触发，在此获取数据
- (void)refreshClick:(UIRefreshControl *)refreshControl {
	[refreshControl beginRefreshing];
	[self getJobInfo];
	//[self.jobListTableView reloadData];// 刷新tableView即可
	[refreshControl endRefreshing];
}
//TableView delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if ([GetUserDefaule(USERINFO)[@"role"] isEqualToString:@"5"] ||
		[GetUserDefaule(USERINFO)[@"role"] isEqualToString:@"6"])
	{
		ApplicantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ApplicantCell"];
		NSLog(@"my role is %@", GetUserDefaule(USERINFO)[@"role"]);
		[cell setJobInfo:self.jobArray[indexPath.row]];
		NSLog(@"求职者名称：%@", cell.name.text);
		return cell;
	}
	else
	{
		JobTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JobCell"];
		if (!cell) {
			// 创建cell,并指定样式和添加重用标识
			cell = [[JobTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"JobCell"];
		}
		NSLog(@"my role is %@", GetUserDefaule(USERINFO)[@"role"]);
		[cell setJobInfo:self.jobArray[indexPath.row]];
		NSLog(@"公司名称：%@", cell.companyName.text);
		return cell;
	}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSString *role = GetUserDefaule(USERINFO)[@"role"];
	/*
	 if ([role integerValue] == 0) {
	 [self presentViewController:[LZCTools alertMessage:@"请先登录"] animated:YES completion:nil];
	 }
	 */
	if ([role isEqualToString:@"5"] ||
		[role isEqualToString:@"6"]) {
		PreviewResumeViewController *resumeVC = [[PreviewResumeViewController alloc] init];
		JobInfoModel *jobInfo = self.jobArray[indexPath.row];
		resumeVC.resumeId = jobInfo.resume_id;
		resumeVC.shouldShowInviteBtn = 1;
		[self.navigationController pushViewController:resumeVC animated:YES];
		
	}
	else
	{
		JobDetailViewController *detail = [[JobDetailViewController alloc] init];
		JobInfoModel *jobInfo = self.jobArray[indexPath.row];
		detail.position_id = jobInfo.position_id;
		detail.shouldHideDeliverBtn = 0;
		[self.navigationController pushViewController:detail animated:YES];
	}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [self.jobArray count];
	
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 170;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 170;
}

- (NSMutableArray *)jobArray
{
	if (!_jobArray) {
		_jobArray = [[NSMutableArray alloc] init];
	}
	return _jobArray;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
